package com.jtjrenren.android.taxi.passenger.core.user.login;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.jtjrenren.android.taxi.passenger.core.user.forgetpwd.UserForgetPwdActivity;
import com.jtjrenren.android.taxi.passenger.core.user.register.UserRegisterActivity;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.base.BaseMvpActivity;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/1/30.
 */
public class UserLoginActivity extends BaseMvpActivity<LoginPresenter> implements LoginView {
    @BindView(R.id.login_pwd_login)
    RadioButton rb_pwd;
    @BindView(R.id.login_phone_no)
    EditText et_phone;
    @BindView(R.id.login_pwd)
    EditText et_pwd;
    @BindView(R.id.login_yzm_input)
    EditText et_yzm;
    @BindView(R.id.login_yzm_desc)
    TextView tv_getyzm;
    @BindView(R.id.login_forget_pwd)
    TextView tv_forgetpwd;
    @BindView(R.id.login_yzm)
    LinearLayout ll_yzm;
    @BindView(R.id.title)
    TextView tv_title;


    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.destory();
    }

    @Override
    public int getLayoutId() {
        return R.layout.aty_user_logins;
    }

    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenterImpl(this);
    }

    @Override
    public void initM() {
        presenter.init();
        rb_pwd.setChecked(true);
        tv_title.setText("登录");
    }


    @OnCheckedChanged(R.id.login_pwd_login)
    void pwdLogin(boolean checked) {
        if (checked)
            presenter.changeLoginType(0);
    }

    @OnCheckedChanged(R.id.login_pwd_yzm)
    void yzmLogin(boolean checked) {
        if (checked)
            presenter.changeLoginType(1);
    }

    @OnClick(R.id.login_forget_pwd)
    public void clickForgetPwd() {
        Intent intent=new Intent(this, UserForgetPwdActivity.class);
        startActivity(intent);
    }


    @OnClick(R.id.login_register_now)
    public void clickGetRegiste() {
        Intent intent = new Intent(this, UserRegisterActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.title_left)
    public void clickBack() {
        finish();
    }


    @OnClick(R.id.login_yzm_desc)
    public void clickGetYzm() {
        String phone = et_phone.getText().toString().trim();
        presenter.beforeSendCode(phone);
    }


    @OnClick(R.id.login_comit)
    public void clickCommit() {
        String phoneno = et_phone.getText().toString();
        String pwd = et_pwd.getText().toString();
        String code = et_yzm.getText().toString();
        presenter.login(phoneno, pwd, code);
    }

    @Override
    public void showPwdLogin() {
        et_pwd.setVisibility(View.VISIBLE);
        ll_yzm.setVisibility(View.GONE);
        tv_forgetpwd.setVisibility(View.VISIBLE);
    }

    @Override
    public void startYzm() {
        tv_getyzm.setEnabled(false);
    }

    @Override
    public void showYzmLogin() {
        ll_yzm.setVisibility(View.VISIBLE);
        et_pwd.setVisibility(View.GONE);
        tv_forgetpwd.setVisibility(View.INVISIBLE);
    }

    @Override
    public void refreshYzmDjs(int process) {
        tv_getyzm.setText(process + getString(R.string.afterrsend));
    }

    @Override
    public void getYzmFinish() {
        tv_getyzm.setEnabled(true);
        tv_getyzm.setText(getString(R.string.getCode));
    }

    @Override
    public void showTip(String desc) {
        Toast.makeText(this,desc,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void finishOk(boolean isOK) {
        if (isOK)
            setResult(RESULT_OK);
        finish();
    }
}
